package zzc.javaWeb.javaWebHomew.dao;

import zzc.javaWeb.javaWebHomew.pojo.LoginBean;
import zzc.javaWeb.javaWebHomew.pojo.PageBean;
import zzc.javaWeb.javaWebHomew.pojo.UserBean;
import zzc.javaWeb.javaWebHomew.utill.DBUTill;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class homeDaoimp implements homeDao{
    DBUTill dbuTill = new DBUTill();

    @Override
    public int tologinUser(LoginBean user) throws SQLException {
        Connection connection = dbuTill.connectDataSource();
        String sql1 = "select*from im_idp where im_id=? and im_password=?";
        PreparedStatement ptmt = connection.prepareStatement(sql1);

        ptmt.setString(1, user.getId());
        ptmt.setString(2, user.getPassword());
        boolean flag1 = ptmt.execute();
        if (flag1 == true) {
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int addloginUser(LoginBean user) throws SQLException {
        Connection connection = dbuTill.connectDataSource();
        String sql1 = "select*from im_idp where im_id=?";
        String sql2 = "INSERT INTO im_idp(im_id,im_password)values(?,?)";
        PreparedStatement ptmt = connection.prepareStatement(sql1);

        ptmt.setString(1, user.getId());
        boolean flag1 = ptmt.execute();
        if (flag1 == false) {
            return 0;
        }else {
            PreparedStatement preparedStatement = connection.prepareStatement(sql2);
            preparedStatement.setString(1,user.getId());
            preparedStatement.setString(2, user.getPassword());

            preparedStatement.execute();
            return 1;
        }
    }

    @Override
    public int findTotalCount() throws SQLException {
        Connection connection = dbuTill.connectDataSource();
        String sql="select*from homeuser";
        PreparedStatement pt = connection.prepareStatement(sql);
        ResultSet rs = pt.executeQuery();
        int count =0;
        while(rs.next()){
            count++;
        }
        System.out.println(count);

        return count;
    }

    @Override
    public List<UserBean> findByPage(PageBean pageBean) throws SQLException {
        Connection connection = dbuTill.connectDataSource();
        UserBean userBean = new UserBean();
        List<UserBean>ubean =new ArrayList<>();
        String sql ="select*from homeuser limit ?,?";
        PreparedStatement pt = connection.prepareStatement(sql);
        pt.setInt(1,pageBean.getDangPage());
        pt.setInt(2,pageBean.getMeiPages());
        ResultSet rs = pt.executeQuery();
        while (rs.next()){
            userBean.setId(rs.getInt("old"));
            userBean.setName(rs.getString("name"));
            userBean.setZhuany(rs.getString("zhuany"));
            userBean.setClassnum(rs.getInt("classnum"));
            userBean.setOld(rs.getInt("old"));
            System.out.println(rs.getString("zhuany"));
            ubean.add(userBean);
        }

        System.out.println(ubean);
        return ubean;
    }

    @Override
    public int addhomePage(UserBean userBean) throws SQLException {
        int flag=0;
        Connection connection = dbuTill.connectDataSource();

        String sql ="insert into homeuser (id,name,zhuany,classnum,old) VALUES (?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userBean.getId());
        ps.setString(2,userBean.getName());
        ps.setString(3,userBean.getZhuany());
        ps.setInt(4,userBean.getClassnum());
        ps.setInt(5,userBean.getOld());
        boolean  flag1 = ps.execute();
        if ( flag1==false){
            flag=1;
        }

        return flag;
    }
}
